﻿Public Class Settings
    Public Shared ReadOnly SetupLevel_Required As Integer = 5

    Private Shared mSettingsRead As Boolean = False
    Private Shared mHost As String
    Private Shared mPort As Integer
    Private Shared mListen As Boolean
    Private Shared mSetupLevel As Integer = 0
    Private Shared mReconnectInterval As Integer = 600
    Private Shared mNickName As String
    Private Shared mEmail As String
    Private Shared mUniqueIdEx As String
    Private Shared mAutoConnect As Boolean

    Public Shared Property Host() As String
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mHost
        End Get
        Set(ByVal value As String)
            If mHost <> value Then
                DataStore.MetaData.SetValue("NetNode_Host", value)
                mHost = value
            End If
        End Set
    End Property
    Public Shared Property UniqueId() As String
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mUniqueIdEx
        End Get
        Set(ByVal value As String)
            If mHost <> value Then
                DataStore.MetaData.SetValue("NetNode_UniqueId", value)
                mHost = value
            End If
        End Set
    End Property

    Public Shared Property Port() As Integer
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mPort
        End Get
        Set(ByVal value As Integer)
            If mPort <> value Then
                DataStore.MetaData.SetValue("NetNode_Port", value)
                mPort = value
            End If
        End Set
    End Property
    Public Shared Property Listen() As Boolean
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mListen
        End Get
        Set(ByVal value As Boolean)
            If mListen <> value Then
                DataStore.MetaData.SetValue("NetNode_Listen", value)
                mListen = value
            End If
        End Set
    End Property
    Public Shared Property AutoConnect() As Boolean
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mAutoConnect
        End Get
        Set(ByVal value As Boolean)
            If mAutoConnect <> value Then
                DataStore.MetaData.SetValue("NetNode_AutoConnect", value)
                mAutoConnect = value
            End If
        End Set
    End Property

    Public Shared Property SetupLevel_Saved() As Integer
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mSetupLevel
        End Get
        Set(ByVal value As Integer)
            If mSetupLevel <> value Then
                DataStore.MetaData.SetValue("NetNode_SetupLevel", value)
                mSetupLevel = value
            End If
        End Set
    End Property

    Public Shared Property ReconnectInterval() As Integer
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mReconnectInterval
        End Get
        Set(ByVal value As Integer)
            If mSetupLevel <> value Then
                DataStore.MetaData.SetValue("NetNode_ReconnectInterval", value)
                mReconnectInterval = value
            End If
        End Set
    End Property

    Public Shared Property Nickname() As String
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mNickName
        End Get
        Set(ByVal value As String)
            If mHost <> value Then
                DataStore.MetaData.SetValue("NetNode_Nickname", value)
                mNickName = value
            End If
        End Set
    End Property
    Public Shared Property Email() As String
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mEmail
        End Get
        Set(ByVal value As String)
            If mEmail <> value Then
                DataStore.MetaData.SetValue("NetNode_Email", value)
                mEmail = value
            End If
        End Set
    End Property


    Private Shared mRSAProvider As System.Security.Cryptography.RSACryptoServiceProvider
    Public Shared ReadOnly Property RSAProvider() As System.Security.Cryptography.RSACryptoServiceProvider
        Get
            Return mRSAProvider
        End Get
    End Property
    Public Shared Sub SetupRSAProvider()
        mRSAProvider = New System.Security.Cryptography.RSACryptoServiceProvider(1024)
        Dim sRsaXml As String = mRSAProvider.ToXmlString(True)
        DataStore.MetaData.SetValue("NetNode_RSAKey", sRsaXml)
    End Sub
    Public Shared Sub SetupUniqueId()
        UniqueId = Utils.Misc.GenerateUniqueId
    End Sub

    Private Shared Sub LoadSettings()
        mHost = DataStore.MetaData.GetValueAsString("NetNode_Host", Nothing)
        mPort = DataStore.MetaData.GetValueAsInteger("NetNode_Port", 0)
        mListen = DataStore.MetaData.GetValueAsBoolean("NetNode_Listen", False)
        mAutoConnect = DataStore.MetaData.GetValueAsBoolean("NetNode_AutoConnect", False)
        mReconnectInterval = DataStore.MetaData.GetValueAsInteger("NetNode_ReconnectInterval", 600)
        mSetupLevel = DataStore.MetaData.GetValueAsInteger("NetNode_SetupLevel", 0)
        mNickName = DataStore.MetaData.GetValueAsString("NetNode_Nickname", System.Environment.UserName & "@" & System.Environment.MachineName)
        mEmail = DataStore.MetaData.GetValueAsString("NetNode_Email", Nothing)
        mUniqueIdEx = DataStore.MetaData.GetValueAsString("NetNode_UniqueId", Nothing)
        Dim sRSAXml As String = DataStore.MetaData.GetValueAsString("NetNode_RSAKey", Nothing)
        If sRSAXml IsNot Nothing Then
            mRSAProvider = New System.Security.Cryptography.RSACryptoServiceProvider
            mRSAProvider.FromXmlString(sRSAXml)
        End If
    End Sub

End Class
